/**
 * 
 */
package info.niwota.pasta;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.speech.RecognizerIntent;
import android.text.ClipboardManager;

/**
 * @author qiangli
 * 
 */
public class Utils {
	//private static final int NOTIFY_ID = R.string.app_name;

	private Utils() {
		//
	}

	/**
	 * 1. replace lf/cr with space 2. trim leading/trailing spaces
	 * 
	 * @param s
	 * @return
	 */
	public static String normalize(String s) {
		if (s == null) {
			return "";
		}
		final String regex = "[\\s]+";
		return s.replaceAll(regex, " ").trim();
	}

	public static boolean canSpeak(Context ctx) {
		PackageManager pm = ctx.getPackageManager();
		List<ResolveInfo> act = pm.queryIntentActivities(new Intent(
				RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
		return (act != null && act.size() > 0);
	}

//	public static void addNotification(Context ctx) {
//		Notification note = new Notification(R.drawable.icon22, null, 0);
//		PendingIntent pi = PendingIntent.getActivity(ctx, 0, new Intent(ctx,
//				NoteListActivity.class), 0);
//		note.flags |= Notification.FLAG_NO_CLEAR;
//		note.flags |= Notification.FLAG_ONGOING_EVENT;
//
//		CharSequence title = ctx.getText(R.string.text_pasta_tagline);
//		CharSequence desc = ctx.getText(R.string.text_pasta_desc);
//		note.setLatestEventInfo(ctx, title, desc, pi);
//		//
//		NotificationManager nm = (NotificationManager) ctx
//				.getSystemService(Context.NOTIFICATION_SERVICE);
//		nm.notify(NOTIFY_ID, note);
//	}
//
//	public static void removeNotification(Context ctx) {
//		NotificationManager nm = (NotificationManager) ctx
//				.getSystemService(Context.NOTIFICATION_SERVICE);
//		nm.cancel(NOTIFY_ID);
//	}

	public static boolean hasClipboardText(Context ctx) {
		ClipboardManager cb = (ClipboardManager) ctx
				.getSystemService(Context.CLIPBOARD_SERVICE);
		return cb.hasText();
	}

	public static void copyToClipboard(Context ctx, String content) {
		ClipboardManager cb = (ClipboardManager) ctx
				.getSystemService(Context.CLIPBOARD_SERVICE);
		cb.setText(content);
	}
}
